import platform,os,tarfile
from confg import *


class base_tool(object):
    def __init__(self):
        pass

    def run(self,cmd):
        r = os.popen(cmd)
        self.readpopen(r)

    def get_filename_with_filepath(self,filepath):
        # this method does not fit windows filepath
        strs = filepath.split("/")
        return strs[len(strs)-1]

    def GetDist(self):
        info = {

        }
        dist = platform.dist()
        arch = platform.architecture()
        info["ostype"] = dist[0]
        info["arch"] = arch[0]
        info["version"] = dist[1].split(".")[0]
        print info
        print platform.dist()
        return info

    def mb_to_gb(self,mb):
        return int(mb)/1024/1024

    def kb_to_mb(self,kb):
        pass

    def kb_to_gb(self,kb):
        pass

    def kb_to_tb(self,kb):
        pass

    def mb_to_tb(self,mb):
        pass

    def gb_to_tb(self,gb):
        pass

    def is_exist(self,filepath):
        return os.path.exists(filepath)

    def download(self,url,save=None):
        # Here it is not easy to wirte a reliable download script in python so using os tool is a great way.
        if save:
            os.popen("wget -c "+url+" -P "+save)
        else:
            os.popen("wget -c "+url)

    def writefile(self,string,filepath):
        with open(filepath,"w") as f:
            f.write(string)

    def readpopen(self,popenobj):
        if dev:
            lines = popenobj.readlines()
            for x in lines:
                print x.strip("\r\n")

    def extract_targz(self,path,save=None):
        filename = self.get_filename_with_filepath(path)
        name = filename.replace(".tar.gz","")
        with tarfile.open(path) as tar:
            if save:
                tar.extract(save)
            else:
                tar.extract(name)





    def get_ram_size(self):
        pass

    def get_disk_size(self):
        pass

    def get_free_disk_size(self):
        pass
